package com.leetcode;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;

public class P0383RansomNote {
	
	
	public static void main(String[] args) {
		
		System.out.println(canConstruct("a", "b"));
		System.out.println(canConstruct("aa", "ab"));
		System.out.println(canConstruct("aa", "aab"));
	}

    public static boolean canConstruct(String ransomNote, String magazine) {
    	
    	char[] str1 = ransomNote.toCharArray();
    	Map<String, Integer> map1 = new HashMap();
    	
    	for(char item : str1) {
    		String temp = String.valueOf(item);
    		if(map1.get(temp) != null) {
    			map1.put(temp, map1.get(temp) + 1);
    		} else {
    			map1.put(temp, 1);
    		}
    	}
    	
    	
    	char[] str2 = magazine.toCharArray();
    	Map<String, Integer> map2 = new HashMap();
    	
    	for(char item : str2) {
    		String temp = String.valueOf(item);
    		if(map2.get(temp) != null) {
    			map2.put(temp, map2.get(temp) + 1);
    		} else {
    			map2.put(temp, 1);
    		}
    	}
    	
    	
    	for(Map.Entry<String, Integer> item : map1.entrySet()) {
    		if(map2.get(item.getKey()) == null || map2.get(item.getKey()) < map1.get(item.getKey())) {
    			return false;
    		}
    	}
    	
    	
    	
		return true;
        
    }
}
